import fs from 'fs'
import { Config, Data, Plugin_Name, Version } from '../../components/index.js'
import puppeteer from '../../../../lib/puppeteer/puppeteer.js'
const _path = process.cwd()

export default new class {
/**
   * @description: 渲染HTML
   * @param {String} path 文件路径
   * @param {Object} params 参数
   * @param {Object} cfg
   */
  async render (path, params, cfg) {
    let [app, tpl] = path.split('/')
    let { e } = cfg
    let layoutPath =
      process.cwd() + `/plugins/${Plugin_Name}/resources/common/layout/`
    let resPath = `../../../../../plugins/${Plugin_Name}/resources/`
    Data.createDir(`data/html/${Plugin_Name}/${app}/${tpl}`, 'root')
    let data = {
      ...params,
      _plugin: Plugin_Name,
      saveId: params.saveId || params.save_id || tpl,
      tplFile: `./plugins/${Plugin_Name}/resources/${app}/${tpl}.html`,
      pluResPath: resPath,
      _res_path: resPath,
      _layout_path: layoutPath,
      _tpl_path:
        process.cwd() + `/plugins/${Plugin_Name}/resources/common/tpl/`,
      defaultLayout: layoutPath + 'default.html',
      elemLayout: layoutPath + 'elem.html',
      pageGotoParams: {
        waitUntil: 'networkidle0'
      },
      sys: {
        scale: this.scale(cfg.scale || 1),
        copyright: `Created By ${Version.name}<span class="version">${Version.yunzai}</span> & Yenai-Plugin<span class="version">${Version.ver}</span>`
      },
      quality: 100
    }
    if (process.argv.includes('web-debug')) {
      // debug下保存当前页面的渲染数据，方便模板编写与调试
      // 由于只用于调试，开发者只关注自己当时开发的文件即可，暂不考虑app及plugin的命名冲突
      let saveDir = _path + '/data/ViewData/'
      if (!fs.existsSync(saveDir)) {
        fs.mkdirSync(saveDir)
      }
      let file = saveDir + tpl + '.json'
      data._app = app
      fs.writeFileSync(file, JSON.stringify(data))
    }
    let base64 = await puppeteer.screenshot(`${Plugin_Name}/${app}/${tpl}`, data)
    let ret = true
    if (base64) {
      ret = await e.reply(base64)
    }
    return cfg.retMsgId ? ret : true
  }

  scale (pct = 1) {
    let scale = Config.whole.renderScale
    scale = Math.min(2, Math.max(0.5, scale / 100))
    pct = pct * scale
    return `style='transform:scale(${pct})'`
  }
}()
